package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**
  * @author Lucie
* @author Lucie
 *class  Delete_Subject_controler
 *Class variables:msg,flag
 *Class methods:delete_subject,check_input,get_all_subjects,String get_msg,set_msg
 */
public class Delete_Subject_controler {

	private String msg; 
	private boolean flag;
	/**
	 * Constructor initializes the class variables
	 */
	public Delete_Subject_controler()
	{
		flag=true;
		msg=new String();
		msg=null;
	}
	
	/**
	 * 
	 *@return String white all of the problems that occurred in the class
	 */
	public String get_msg()
	{
		return msg;
	}
	/**
	 * This method chain messages into a string ,
	 * the purpose ultimately is to display on the screen all the failures
	 * @param new_msg
	 */
	public void set_msg(String new_msg)
	{
		if (msg==null)
			msg=new_msg;
		else
		msg+=new_msg;
		
	}
	
/**
 * this  method call the class ;serverproxy, to Contact with the server,
 * we call the method getSubjects() that return from the db all the subjects that exist in the db
 *  
 * @return ArrayList<String> with all the subjects names and id
 */
	public   ArrayList<String> get_all_subjects()
	{
		 ArrayList<String> str_subjects=new  ArrayList<String>(); 

		str_subjects=BusinessImplements.GetInstance().getServerProxy().getSubjects();
		
		
		return str_subjects;
		
		
	}
	
	/**
	 * 
	 * @param subject_id
	* this  method call the class ;serverproxy, to Contact with the server,
	 * first we check the Parameters we get (check_input) if something is wrong we return false 
	 *than we call the method count_subjectsitems that check if the subject have items that relate to his and if he thus we return false
	 *and set an error msg  if everything sucsed  we delete the subject to the db (delete_subject) if something go wrong we return false 
	 * @return  boolean:If the calls in the method failed it returns false otherwise returns true
	 */
	 public boolean delete_subject(String subject_id)
		{
		

		 ArrayList<String> str_check= new  ArrayList<String>();
		 	ArrayList<String> str_subject=new  ArrayList<String>();
	 
		 check_input(subject_id);
		 	if (flag==false)
		 return false;
		 	
		 	str_subject=BusinessImplements.GetInstance().getServerProxy().count_subjectsitems(subject_id);
			
		 	if (!str_subject.get(0).equals("0"))
		 	{
		 		set_msg("\nthe chosen subject have "+str_subject.get(0)+" items that related to him\nif  you like to delete the subject first delete all the item he relate to"); 
		 		return false;
		 	}
		 	
		 	str_check=BusinessImplements.GetInstance().getServerProxy().delete_subject(subject_id);
		 	if (str_check.get(0).equals("false"))
		 		set_msg("\ndeleted faild"); 
		 	return flag;
		 	
		 	
}
	/**
	 * This method checks the parameters we have received are not empty
	  * if they are poses flag= false;
	 * @param subject_id
	 */
		public void check_input(String subject_id)
		{
			
			if(subject_id.equals(""))
			{
				set_msg("\nmising subject name ");
				flag=false;
			}
		
			
		}

}
